<?php

/**
 * uniBoard ACP Edit Mask
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_mask{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore			= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api				= NULL;
	
	/**
	 * Permissions
	 *
	 * @var array
	 */
	
	private $permissions		= array();
	
	/**
	 * Mask Perms
	 *
	 * @var array
	 */
	
	private $mask_permissions	= array();
	
	/**
	 * Forums
	 *
	 * @var array
	 */
	
	private $forums				= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_permission_mask( $appCore, $appCore -> _REQ( 'mask'));
			
		// mask NOT found?
		if ( !$this -> api -> data_loaded)
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['acp_mask_not_found'], $appCore -> lang['acp_access_masks']);
			
			// Jump back to Masks List
			new action_masks( $appCore);
		}
		else if ( $appCore -> _REQ( 'do') == 'name')
		{
			// Update?
			if ( $appCore -> _POST( 'update') == 1)
			{
				// Fill Api
				$this -> api -> setMaskName( $appCore -> _POST( 'mask_name'));
				
				// Name is good?
				if ( $this -> api -> error)
				{
					// Draw Errors
					$appCore -> output -> error( $this -> api -> last_error, $appCore -> lang['acp_mask_op_rename']);
					
					// Repeat Form
					$this -> drawNameForm();
				}
				else
				{
					// Save Api
					$this -> api -> saveData();
					
					// Set Log
					$appCore -> logAdmin( 'log_admin_mask_name_changed', htmlspecialchars($this -> api -> mask_name));
					
					// Draw Message
					$appCore -> output -> message( $appCore -> lang['acp_mask_name_changed'], $appCore -> ifString( htmlspecialchars( $this -> api -> mask_name)));
					
					// Jump back to Masks List
					new action_masks( $appCore);
				}
			}
			else
			{
				// Draw Form
				$this -> drawNameForm();
			}
		}
		
		else if ( $appCore -> _REQ( 'do') == 'copy')
		{
			// Update?
			if ( $appCore -> _POST( 'update') == 1)
			{
				// Fill Api
				$this -> api -> setMaskName( $appCore -> _POST( 'mask_name'));
				$this -> api -> setMaskPermissions( $this -> api -> mask_permissions);
				
				// Name is good?
				if ( $this -> api -> error)
				{
					// Draw Errors
					$appCore -> output -> error( $this -> api -> last_error, $appCore -> lang['acp_mask_op_copy']);
					
					// Repeat Form
					$this -> drawCopyForm();
				}
				else
				{
					// Copy It
					$this -> api -> mask_id = 0;
					
					// Save Api
					$this -> api -> saveData();
					
					// Set Log
					$appCore -> logAdmin( 'log_admin_mask_copied', htmlspecialchars( $appCore -> _POST( 'mask_name')));
					
					// Redirect user
					$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=masks'), $appCore -> lang['acp_mask_copy_saved']);
				}
			}
			else
			{
				// Draw Form
				$this -> drawCopyForm();
			}
		}
		else if ( $appCore -> _REQ( 'do') == 'main')
		{
			// Load Permissions
			$this -> permissions = $appCore -> getPermissions();
			$this -> mask_permissions = $this -> api -> mask_permissions;
			
			// Update Perms?
			if ( $appCore -> _POST( 'perms_main') == 1)
			{
				// Reiterate Mask\'s Perms
				foreach ( $this -> permissions as $perm_id => $perm_data) 
				{
					// Perm in new array?
					if ( strlen( $appCore -> _POST( 'permission_' . $perm_id)) > 0)
					{
						// Perm Type?
						switch ( $perm_data['permission_type'])
						{
							// Yes No
							case 0:
								// Bool Value
								$this -> mask_permissions[$perm_id] = (bool) $appCore -> _POST( 'permission_' . $perm_id);
							break;
							
							// Text
							case 1:
								// Get Value turned into integer
								$this -> mask_permissions[$perm_id] = (integer) $appCore -> _POST( 'permission_' . $perm_id);
								
								// Check Min Value?
								if ( isset( $perm_data['permission_extra']['MIN_VAL']) && $perm_data['permission_extra']['MIN_VAL'] > $this -> mask_permissions[$perm_id])
								{
									// Make Perm Same as Minimal Value
									$this -> mask_permissions[$perm_id] = $perm_data['permission_extra']['MIN_VAL'];
								}
								
								// Check Max Value?
								if ( isset( $perm_data['permission_extra']['MAX_VAL']) && $perm_data['permission_extra']['MAX_VAL'] < $this -> mask_permissions[$perm_id])
								{
									// Make Perm Same as Maximal Value
									$this -> mask_permissions[$perm_id] = $perm_data['permission_extra']['MAX_VAL'];
								}
							break;
							
							// List
							case 2:
								// Get Value turned into integer
								$this -> mask_permissions[$perm_id] = (integer) $appCore -> _POST( 'permission_' . $perm_id);
								
								// Is it improper proper key from perms list?
								if ( !isset( $perm_data['permission_extra'][$this -> mask_permissions[$perm_id]]))
								{
									// Set Default Value
									$this -> mask_permissions[$perm_id]	= $perm_data['permission_default'];
								}
							break;
						}
					}
					else
					{
						// Set Default Value
						$this -> mask_permissions[$perm_id]	= $perm_data['permission_default'];
					}
				}
				
				// Update Perms
				$this -> api -> setMaskPermissions( $this -> mask_permissions);
				
				// Save Api
				$this -> api -> saveData();	
									
				// Set Log
				$appCore -> logAdmin( 'log_admin_mask_perms_main_changed', htmlspecialchars($this -> api -> mask_name));
				
				// Redirect user
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=masks'), $appCore -> lang['acp_mask_perms_main_saved']);
			}
			else
			{
				// Draw Permissions Form
				$this -> drawPermsMainForm();
			}
		}
		else if ( $appCore -> _REQ( 'do') == 'forums')
		{
			// Fix Mask Stuff
			if ( !isset($this -> api -> mask_permissions['forum_show']))
			{
				$this -> api -> mask_permissions['forum_show'] = array();
			}
			
			if ( !isset($this -> api -> mask_permissions['forum_read']))
			{
				$this -> api -> mask_permissions['forum_read'] = array();
			}
			
			if ( !isset($this -> api -> mask_permissions['topics_start']))
			{
				$this -> api -> mask_permissions['topics_start'] = array();
			}
			
			if ( !isset($this -> api -> mask_permissions['topics_reply']))
			{
				$this -> api -> mask_permissions['topics_reply'] = array();
			}
			
			if ( !isset($this -> api -> mask_permissions['files_upload']))
			{
				$this -> api -> mask_permissions['files_upload'] = array();
			}
			
			if ( !isset($this -> api -> mask_permissions['files_download']))
			{
				$this -> api -> mask_permissions['files_download'] = array();
			}
			
			// Load Permissions
			$this -> mask_permissions = $this -> api -> mask_permissions;
						
			// Load Forums
			$this -> forums = $appCore -> getForums( true);
			
			// Update Perms?
			if ( $appCore -> _POST( 'perms_forums') == 1)
			{
				// Set Stuff
				$this -> mask_permissions['forum_show'] = $appCore -> listInput( 'forum_show');
				$this -> mask_permissions['forum_read'] = $appCore -> listInput( 'forum_read');
				$this -> mask_permissions['topics_start'] = $appCore -> listInput( 'topics_start');
				$this -> mask_permissions['topics_reply'] = $appCore -> listInput( 'topics_reply');
				$this -> mask_permissions['files_upload'] = $appCore -> listInput( 'files_upload');
				$this -> mask_permissions['files_download'] = $appCore -> listInput( 'files_download');
				
				// Update Perms
				$this -> api -> setMaskPermissions( $this -> mask_permissions);
				
				// Save Api
				$this -> api -> saveData();	
									
				// Set Log
				$appCore -> logAdmin( 'log_admin_mask_perms_forums_changed', htmlspecialchars($this -> api -> mask_name));
				
				// Redirect user
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=masks'), $appCore -> lang['acp_mask_perms_forums_saved']);
			}
			else
			{
				// Draw Permissions Form
				$this -> drawPermsForumsForm();
			}
		}
		else if ( $appCore -> _REQ( 'do') == 'delete')
		{
			// Any group using this mask?
			if ( $appCore -> db -> count( 'user_groups', 'group_masks LIKE \'' . $this -> api -> mask_id . '\' OR group_masks LIKE \'' . $this -> api -> mask_id . ',%\' OR group_masks LIKE \'%,' . $this -> api -> mask_id . '\' OR group_masks LIKE \'%,' . $this -> api -> mask_id . ',%\'') > 0)
			{
				// Draw Errors
				$appCore -> output -> error( $appCore -> lang['acp_mask_cant_delete'], $appCore -> ifString( htmlspecialchars( $this -> api -> mask_name)));
				
				// Jump back to Masks List
				new action_masks( $appCore);
			}
			else
			{
				// Delete Api
				$this -> api -> deleteData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_mask_deleted', htmlspecialchars( $this -> api -> mask_name));
				
				// Draw Message
				$appCore -> output -> message( $appCore -> lang['acp_mask_deleted'], $appCore -> ifString( htmlspecialchars( $this -> api -> mask_name)));
				
				// Jump back to Masks List
				new action_masks( $appCore);
			}
		}
		else
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['wrong_action_code'], $appCore -> lang['acp_access_masks']);
			
			// Jump back to Masks List
			new action_masks( $appCore);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Name Form
	 *
	 */
	
	function drawNameForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_mask&do=name&mask=' . $this -> api -> mask_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> ifString( htmlspecialchars( $this -> api -> mask_name)));
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_mask_form_name'], $this-> appCore -> drawTextInput( 'mask_name', $this -> api -> mask_name), $this -> appCore -> lang['acp_mask_form_name_expl']);
				
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_mask_op_rename'], array(
			$this -> appCore -> adminLink( 'act=masks') => $this -> appCore -> lang['acp_access_masks'],
			'' => $this -> appCore -> ifString( htmlspecialchars( $this -> api -> mask_name))
		), ob_get_clean());
			
	}

	// ------------------------------------------------------------------------

	/**
	 * Draws Copy Form
	 *
	 */
	
	function drawCopyForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_mask&do=copy&mask=' . $this -> api -> mask_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> ifString( htmlspecialchars( $this -> api -> mask_name)));
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_mask_form_new_name'], $this-> appCore -> drawTextInput( 'mask_name', $this -> api -> mask_name), $this -> appCore -> lang['acp_mask_form_name_expl']);
				
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['create_copy']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_mask_op_copy'], array(
			$this -> appCore -> adminLink( 'act=masks') => $this -> appCore -> lang['acp_access_masks'],
			'' => $this -> appCore -> ifString( htmlspecialchars( $this -> api -> mask_name))
		), ob_get_clean());
			
	}

	// ------------------------------------------------------------------------

	/**
	 * Main Permissions Form
	 *
	 */
	
	function drawPermsMainForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_mask&do=main&mask=' . $this -> api -> mask_id));
		echo $this -> appCore -> drawHidden( 'perms_main', 1);
		
		// Block Opened?
		$in_block = false;
		
		// Reiterate Blocks
		foreach ( $this -> permissions as $perm_id => $perm_data)
		{
			// Open block?
			if ( isset($perm_data['permission_section'][0]))
			{
				// Close Previous Block?
				if ( $in_block)
				{
					echo $this -> appCore -> output -> closeFormBlock();
				}
				
				// Open New Block
				echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> ifString( $perm_data['permission_section']));
				
				// We are in block
				$in_block = true;
			}
			
			// Field value?
			if ( !isset( $this -> mask_permissions[$perm_id]))
			{
				$perm_value = $perm_data['permission_default'];
			}
			else
			{
				$perm_value = $this -> mask_permissions[$perm_id];
			}
			
			// Draw Field
			switch ( $perm_data['permission_type'])
			{
				// Yes-No
				case 0:
					$form_input = $this -> appCore -> drawYesNo( 'permission_' . $perm_id, $perm_value);
				break;
				
				// Text
				case 1:
					$form_input = $this -> appCore -> drawTextInput( 'permission_' . $perm_id, $perm_value);
				break;
				
				// List
				case 2:
					$form_input = $this -> appCore -> drawList( 'permission_' . $perm_id, $perm_data['permission_extra'], $perm_value);
				break;
			}
			
			// Draw Perm Form
			echo $this -> appCore -> output -> formItem( $this -> appCore -> ifString( $perm_data['permission_name']), $form_input, $this -> appCore -> ifString( $perm_data['permission_description']));
		}
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_mask_op_perms_main'], array(
			$this -> appCore -> adminLink( 'act=masks') => $this -> appCore -> lang['acp_access_masks'],
			'' => $this -> appCore -> ifString( htmlspecialchars( $this -> api -> mask_name))
		), ob_get_clean());
			
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Forums Permissions Form
	 *
	 */
	
	function drawPermsForumsForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
			
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_mask&do=forums&mask=' . $this -> api -> mask_id));
		echo $this -> appCore -> drawHidden( 'perms_forums', 1);
		
		// Reiterate forums
		foreach ( $this -> forums as $forum_id => $forum_name)
		{
			// Open Block
			echo $this -> appCore -> output -> openTableBlock($forum_name, '<a href="javascript:massSelect(\'forum_perms_' . $forum_id . '\')">' . $this -> appCore -> lang['acp_mask_form_select_all'] . '</a> / <a href="javascript:massClear(\'forum_perms_' . $forum_id . '\')">' . $this -> appCore -> lang['acp_mask_form_select_none'] . '</a>');
			
			// Open Container
			echo '<div id="forum_perms_' . $forum_id . '">';
			
			// Draw Head
			echo $this -> appCore -> output -> tableHeader('<table style="table-layout: fixed;">
	<tr>
		<td class="cell_pad center">
			' . $this -> appCore -> lang['acp_mask_form_perm_show_forum'] . '
		</td>
		<td class="cell_pad center">
			' . $this -> appCore -> lang['acp_mask_form_perm_read_forum'] . '
		</td>
		<td class="cell_pad center">
			' . $this -> appCore -> lang['acp_mask_form_perm_topics_start'] . '
		</td>
		<td class="cell_pad center">
			' . $this -> appCore -> lang['acp_mask_form_perm_topics_reply'] . '
		</td>
		<td class="cell_pad center">
			' . $this -> appCore -> lang['acp_mask_form_perm_files_upload'] . '
		</td>
		<td class="cell_pad center">
			' . $this -> appCore -> lang['acp_mask_form_perm_files_download'] . '
		</td>
	</tr>
</table>');
			
			// Draw Row
			echo $this -> appCore -> output -> tableRow('<table style="table-layout: fixed;">
	<tr>
		<td class="cell_pad center">
			' . $this -> appCore -> drawCheckbox('forum_show[' . $forum_id . ']', in_array($forum_id, $this -> mask_permissions['forum_show'])) . '
		</td>
		<td class="cell_pad center">
			' . $this -> appCore -> drawCheckbox('forum_read[' . $forum_id . ']', in_array($forum_id, $this -> mask_permissions['forum_read'])) . '
		</td>
		<td class="cell_pad center">
			' . $this -> appCore -> drawCheckbox('topics_start[' . $forum_id . ']', in_array($forum_id, $this -> mask_permissions['topics_start'])) . '
		</td>
		<td class="cell_pad center">
			' . $this -> appCore -> drawCheckbox('topics_reply[' . $forum_id . ']', in_array($forum_id, $this -> mask_permissions['topics_reply'])) . '
		</td>
		<td class="cell_pad center">
			' . $this -> appCore -> drawCheckbox('files_upload[' . $forum_id . ']', in_array($forum_id, $this -> mask_permissions['files_upload'])) . '
		</td>
		<td class="cell_pad center">
			' . $this -> appCore -> drawCheckbox('files_download[' . $forum_id . ']', in_array($forum_id, $this -> mask_permissions['files_download'])) . '
		</td>
	</tr>
</table>');
			
			// Close Container
			echo '</div>';
			
			// Close Block
			echo $this -> appCore -> output -> closeTableBlock();
		}
		
		// Close Form
		echo $this -> appCore -> output -> openFormBlock();
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_mask_op_perms_forums'], array(
			$this -> appCore -> adminLink( 'act=masks') => $this -> appCore -> lang['acp_access_masks'],
			'' => $this -> appCore -> ifString( htmlspecialchars( $this -> api -> mask_name))
		), ob_get_clean());
			
	}
	
}